package com.huatai.exchange.deal;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.Constants;
import com.faneui.common.utils.ListUtils;
import com.google.common.collect.Lists;
import com.huatai.exchange.dal.model.ExcDataFileInfo;
import com.huatai.exchange.dal.model.ExcFileLoadInfo;
import com.huatai.exchange.dal.model.ExcFileLoadRecord;
import com.huatai.exchange.facade.Contents;
import com.huatai.exchange.facade.FileSourceConstants;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("fileCreateLoadDealService")
public class FileCreateLoadDealServiceImpl extends AbstractFileCreateDealServiceImpl implements FileCreateDealService {

    @Override
    void generate(Map<String, Object> valueMap, ExcDataFileInfo dataFileInfo, StringBuffer fileNames, StringBuffer filePaths) throws BusinessException {
        String fileName = formatStr(dataFileInfo.getFileName(), valueMap);
        String loadFileId = formatStr((String) valueMap.get(FileSourceConstants.LOAD_FILE_ID), valueMap);
        String occurDate = (String) valueMap.get(Contents.FIELD_YYYYMMDD);

        ExcFileLoadInfo loadInfo = excFileLoadInfoService.queryExcFileLoadInfo(loadFileId) ;
        if(loadInfo == null) {
            throw new BusinessException("ID【"+loadFileId+"】文件不存在") ;
        }
        ExcFileLoadRecord record = excFileLoadRecordService.queryExcFileLoadRecord(loadFileId, occurDate);
        if (record == null) {
            throw new BusinessException("文件["+loadInfo.getFileName()+"]未准备好");
        }
        if("-".equals(record.getFilePath())){
            // 当日无文件
            return;
        }

        if (valueMap.get(FileSourceConstants.INNER_FILE_NAME) == null) {
            fileNames.append("," + fileName);
            filePaths.append("," + record.getFilePath());
        } else {
            // 提取 压缩文件中的文件 名的文件标志
            String innerFileName = formatStr((String) valueMap.get(FileSourceConstants.INNER_FILE_NAME), valueMap);
            ExcFileLoadInfo info = excFileLoadInfoService.queryExcFileLoadInfo(record.getLoadId()) ;
            // 说明是多个文件
            logger.info("处理多个文件");
            if(innerFileName.contains("*")) {
                List<String> paths = Lists.newArrayList() ;
                List<String> names = Lists.newArrayList() ;

                if(info.getSrcName().contains(".zip") || Constants.boolean_true.equals(info.getMoreFlag())) {
                    //      使用java 进行加压
                    fileOperateService.unZipAndGetFilePathsAndNames(record.getFilePath(),innerFileName,paths,names);

                }else if(info.getSrcName().contains(".rar")) {
                    //      使用远程解压
                    //TODO
                }
                if(ListUtils.isEmpty(paths)) {
                    logger.info("文件错误,无");
                    throw new BusinessException("文件错误,无") ;
                }
                for(int i=0;i<paths.size();i++){
                    filePaths.append(",").append(paths.get(i)) ;
                    fileNames.append(",").append(names.get(i)) ;
                    valueMap.put(Contents.FILE_GC, valueMap.get(Contents.FILE_GC) + "," + paths.get(i));
                }
            }else {
                fileNames.append("," + fileName);
                String resultFilePath = "" ;
                if(info.getSrcName().contains(".zip")) {
                    //      使用java 进行加压
                    resultFilePath =  fileOperateService.unZipAndGetFile(record.getFilePath(),innerFileName) ;
                }else if(info.getSrcName().contains(".rar")) {
                    //      使用远程解压
                    resultFilePath = fileOperateService.unRarAndGetFile(record.getFilePath(),innerFileName) ;
                }
                filePaths.append(",").append(resultFilePath) ;
                valueMap.put(Contents.FILE_GC, valueMap.get(Contents.FILE_GC) + "," + resultFilePath);
            }

        }
    }
}
